<?php

    require_once '../../model/global.inc.php'; 
  //check to see that the form has been submitted    
    
		//retrieve the $_POST variables  
		$username = $_POST['email'];  
		$password = $_POST['password'];  
		$password_confirm = $_POST['password-confirm'];  
		$email_confirm = $_POST['email-confirm'];  
                $firstname = $_POST['firstname'];  
                $lastname = $_POST['lastname']; 
                $country_name = $_POST['country'];                
		//initialize variables for form validation  
		$success = true;  
		$userTools = new UserTools();  
                
                if($userTools->checkUsernameExists($username) == true)
                {                   
                    $success = false;  
                }
                
                if(isset($country_name))
                        {
                            $result = mysql_query("SELECT country_id FROM tblcountry where country_name = '$country_name'");

                            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
                            { 
                                    $country_id = $row["country_id"];

                            }
                        }
		//validate that the form was filled out correctly  
                
                        
                if($username == '' || $password == '' || $password_confirm='' || $email_confirm='')
                {
                         echo "<script>alert('Please enter imformation');</script>"; 
			$success = false;  
                }
                
                if($password != $password_confirm || $email_confirm != $username)
                {
                    $success = FALSE;
                }
                
                $myReg="/^[A-Za-z0-9\._-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)$/";
                if(preg_match($myReg, $username)){
                    $success = true;  
                }else{
                    $success = false;  
                }

                if(strlen($firstname) >20 )
                {
                        echo "<script>alert('Firstname is not more 20 characters');</script>";     
			$success = false;
                }
                
                if(!isset($_POST['agree']))
                {                           
			$success = false; 
                }
     
		if($success)  
		{  
			//prep the data for saving in a new user object  
			$data['email'] = $username;  
			$data['password'] = md5($password); //encrypt the password for storage  
                        $data['firstname'] = $firstname;
                        $data['lastname'] = $lastname;                        
                        $data['country_id'] = $country_id;
   
			//create the new user object  
			$newUser = new User($data);  
     
			//save the new user to the database  
			$newUser->save(true);  
    
			//log them in  
			$userTools->login($username, $password);  
    
			echo "<script>window.location='index.php';</script>";
     
		}  
     
	//If the form wasn't submitted, or didn't validate  
	//then we show the registration form again  

?>
